import requests
import base64
import subprocess
import time
import re
# 导入js文件
headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Origin": "https://www.qimai.cn",
    "Pragma": "no-cache",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0",
    "sec-ch-ua": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Microsoft Edge\";v=\"132\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
a=time.time()
synct = "{:.3f}".format(a)
# print(synct)
data=[synct,'']
result = subprocess.run(['node', '1.js'] + data, capture_output=True, text=True)
# 输出结果,转化为列表
# print("JavaScript脚本输出:", result.stdout)
# 匹配引号之间的字符串
list= re.findall(r"'(.*?)'", result.stdout)

encoded_bytes = base64.b64encode(list[0].encode('utf-8'))
encoded_str = encoded_bytes.decode('utf-8')
# print("Base64 String:", encoded_str)
# as='dkZJFQkWElVfUwYcMVVDFkkFGhkXHmpYU0xVIRoBUlFRWExPTQUJDyNHUg'
# qw='dkZJXHgxNVx4MDlceDE2XHgxMlVcX1NceDA2XHgxYzFVQ1x4MTZJXHgwNVx4MWFceDE5XHgxN1x4MWVqWFNMVSFceDFhXHgwMVJRUVhMT01ceDA1XHgwOVx4MGYjR1I='
cookies = {
    "ada35577182650f1_gr_last_sent_cs1": "qm23884719561",
    "USERINFO": "CcHLxJE6XE2ZPNEz%2B9NWOHJrJPP3XqO7mCpFom%2FUyjj%2FKUgBq6prgUCjyZREoKg3drqA6nVE3w2LXGEoryeusdwWYAkR81K1yMKS71y730KMa3OsTFs0sMyHMTriJvmKOAPZvMjQU%2FpzP0LoNZ5UesHoejR6lWcr",
    "ada35577182650f1_gr_cs1": "qm23884719561",
    "synct": str(synct),
    "syncd": "-15658",
    "PHPSESSID": "dvpg5brthhu3bprssi2b5nvkg2",
    "qm_check": "A1sdRUIQChtxen8pI0dAMRcOUFseEHBeQF0JTjVBWDAIXEQaYhAQbF1FIRUJCBETVkQSGAlIBAhVVlNeOU9TFXNbQlxTQAshV1ZIDgolAGgCEElDaw06VktIPEo+BAYbEhUSV1AABQxKQltKGQceABUAGAhHGw%3D%3D"
}
url = "https://api.qimai.cn/rank/indexPlus/brand_id/1"
# 0：付费榜、1：免费榜、2：畅销榜
params = {
    "analysis": str(encoded_str),
    "brand": "all",
    "device": "iphone",
    "country": "cn",
    # 英国：gb，中国：cn，美国：us，日本：jp，韩国：kr，台湾：tw，香港：hk
    "genre": "36",
    "date": "2025-02-08",
    "page": "1"
}
response = requests.get(url, headers=headers, cookies=cookies, params=params)
print(response)
item={}
for i in range(len(response.json()["list"])):
    res=response.json()["list"][i]["appInfo"]["appName"]
    print(res)
